//
//  GameXiangQingViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/3/4.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "GameXiangQingViewController.h"
#import "Game4TableViewCell.h"
#import "GameXangQing.h"
#import "GameXiangQingPingLun.h"
#import "GameZanTableViewCell.h"
#import "GameComTableViewCell.h"
@interface GameXiangQingViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
@property (nonatomic, retain) UIImageView *imageView2;

@property (nonatomic, assign) CGFloat realHeight1;
@property (nonatomic, assign) CGFloat realHeight;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UINavigationController *navigationController;
@property (nonatomic, retain) UIImageView *image1;


@property (nonatomic, retain) UITextField *textFiled;
@property (nonatomic, retain) UIView *backView;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UIImageView *imageView;

@property (nonatomic, retain) NSDictionary *dicAll;/**<  接受总数据的字典 */
@property (nonatomic, retain) NSMutableArray *arrLike;/**<  装喜欢的数组 */
@property (nonatomic, retain) NSMutableArray *arrCom;/**<  评论的数组 */
@end

@implementation GameXiangQingViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_imageView2 release];
    [_tableView release];
    [_navigationController release];
    [_image1 release];
    [_textFiled release];
    [_backView release];
    [_button release];
    [_imageView release];
    [_dicAll release];
    [_arrLike release];
    [_arrCom release];
    [_model release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
    view.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    
    [self.view addSubview:view];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 30, 20, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-jiantouzuo-2.png"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    
    UILabel *label = [[UILabel alloc] init];
    
    label.frame = CGRectMake(0, 25, 200, 50);
    label.text = @"游记详情";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:18];
    label.textAlignment = NSTextAlignmentCenter;
    label.center = view.center;
    [view addSubview:label];
    
    [label release];
    
    
    
    [self createTableVIew];
    
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40)];
    _backView.backgroundColor = [UIColor lightGrayColor];
    _backView.alpha = 0.9;
    _backView.layer.cornerRadius = 2;
    _backView.layer.masksToBounds = YES;
    _backView.layer.borderColor = [UIColor grayColor].CGColor;
    _backView.layer.borderWidth = 1;
    [self.view addSubview:_backView];
    

    self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, self.view.frame.size.width - 80, 30)];
    _textFiled.backgroundColor = [UIColor whiteColor];
    _textFiled.tintColor = [UIColor whiteColor];
    _textFiled.borderStyle = UITextBorderStyleRoundedRect;
    _textFiled.placeholder = @"与我互动";
    _textFiled.delegate = self;
    [_backView addSubview:_textFiled];
    [_textFiled release];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(self.view.frame.size.width - 50, 5, 40, 30);
    _button.backgroundColor = [UIColor whiteColor];
    
    _button.layer.cornerRadius = 5;
    _button.layer.masksToBounds = YES;
    _button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _button.layer.borderWidth = 1;

    [_button addTarget: self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_backView addSubview:_button];
    
    
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 20, 20)];
    _imageView.backgroundColor = [UIColor clearColor];
    
    UIImage *image = [UIImage imageNamed:@"iconfont-shoucang-4.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    _imageView.image = image;
    [_button addSubview:_imageView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonAction)];
    
    [_imageView addGestureRecognizer:tap];
    
    
    
    [self handelData];
    
}
//返回
- (void)buttonAction:(UIButton *)button{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

- (void)buttonAction{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 40, self.view.frame.size.height - 30, 20, 20)];
    label.backgroundColor = [UIColor clearColor];
    label.layer.cornerRadius = 10;
    label.layer.masksToBounds = YES;
    label.text = @"+1";
    label.textColor = [UIColor redColor];
    label.tag = 1000;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:12];
    [self.view addSubview:label];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    
    [UIView animateWithDuration:2 animations:^{
        
        label.transform = CGAffineTransformMakeScale(2.5, 2.5);
        
        animation.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(label.center.x - 20, label.center.y - 10)],
                            [NSValue valueWithCGPoint:CGPointMake(label.center.x - 30, label.center.y - 40)],
                            [NSValue valueWithCGPoint:CGPointMake(label.center.x - 40, label.center.y - 60)],
                            [NSValue valueWithCGPoint:CGPointMake(label.center.x - 55, label.center.y - 80)],
                            [NSValue valueWithCGPoint:CGPointMake(label.center.x - 60, label.center.y - 100)],
                            [NSValue valueWithCGPoint:CGPointMake(label.center.x - 80, label.center.y - 120)],nil];
        animation.duration = 3;
        [label.layer addAnimation:animation forKey:nil];
        
        
    } completion:^(BOOL finished) {
        label.hidden = YES;
        
        _button.backgroundColor = [UIColor redColor];
        
        
        

    }];

}



- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    [UIView animateWithDuration:0.3 animations:^{
        _backView.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40);
    }];
    
    return YES;
}



- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    [UIView animateWithDuration:0.3 animations:^{
        _backView.frame = CGRectMake(0, self.view.frame.size.height-216 - 80, self.view.frame.size.width, 40);
    }];
    
    return YES;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [_textFiled resignFirstResponder];
    [UIView animateWithDuration:0.3 animations:^{
        _backView.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40);
    }];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        cell.layer.transform = CATransform3DMakeRotation(2, 0, 1, 0);
        [UIView animateWithDuration:0.5 animations:^{
            cell.layer.transform = CATransform3DMakeRotation(0, 0, 0, 0);
        }];
        
    }
    
}

- (void)cancelAction:(UIBarButtonItem *)item{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)createTableVIew{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, self.view.frame.size.width, self.view.frame.size.height-80 ) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
    [_tableView release];
    
    
    [_tableView registerClass:[Game4TableViewCell class] forCellReuseIdentifier:@"Game4TableViewCell"];
    
    [_tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"BaseTableViewCell"];
    
    [_tableView registerClass:[GameZanTableViewCell class] forCellReuseIdentifier:@"GameZanTableViewCell"];
    
    [_tableView registerClass:[GameComTableViewCell class] forCellReuseIdentifier:@"GameComTableViewCell"];
    
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [_textFiled resignFirstResponder];
    [UIView animateWithDuration:0.3 animations:^{
        _backView.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40);
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4 + _arrCom.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 0) {
        Game4TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Game4TableViewCell"];
        
        
        cell.model = _model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        return cell;
        
    }else if (indexPath.row == 1){
        
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell"];
        
        NSString *text = [NSString stringWithFormat:@"%@ 人喜欢", _dicAll[@"recommender_count"]];
        
        cell.textLabel.text = text;
        cell.textLabel.textColor = [UIColor redColor];
        return cell;
        
    }else if (indexPath.row == 2 ){
        
        
        
        GameZanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GameZanTableViewCell"];
       
        cell.arr = _arrLike;
        
        return cell;
        
    }else if (indexPath.row == 3){
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell"];
        
        NSString *text = [NSString stringWithFormat:@"%@ 人评价", _dicAll[@"comment_count"]];
        cell.textLabel.text = text;
        cell.textLabel.textColor = [UIColor redColor];
        
        return cell;
        
    }else{
        GameComTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GameComTableViewCell"];
        GameXiangQingPingLun *model = _arrCom[indexPath.row - 4];
        cell.model = model;
        return cell;
    }
    
    
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        //获取图片的真实高度
        NSDictionary *dInfo = _model.photo_info;
        NSNumber *num1 = dInfo[@"h"];
        NSNumber *num2 = dInfo[@"w"];
        CGFloat height = [num1 floatValue];
        CGFloat width =  [num2 floatValue];
        self.realHeight = 0;
        if ([num1 integerValue] > 0 && [num2 integerValue] > 0) {
            
            //缩放后宽度固定为335；
            CGFloat scale = width / 355;
            _realHeight = height / scale;
            _imageView2.frame = CGRectMake(10, 50, 355, _realHeight);
            
            NSLog(@"flllllll");
            [_imageView2 sd_setImageWithURL:[NSURL URLWithString:_model.photo] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
            
        }
        
        NSString *str4 = _model.text;
        //设置字体样式
        NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        
        CGRect rect = [str4 boundingRectWithSize:CGSizeMake(340, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:att context:nil];
        
        return _realHeight + 50 + 5 + rect.size.height+ 65;
        
    }else if (indexPath.row == 1){
        return 80;
    }else{
        return 80;
    }
    
    

}


- (void)handelData{
    NSString *url = [_model.id stringValue];
    
    
    NSString *result = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/2387319416/waypoints/%@/replies/?count=2147483647&sign=3acd7fb9f6dff34bbaf3790dbc5f22fc", url];
    
    
    [AFNetWorkingHandle GETRequestWithURLString:result completionWithSuccess:^(id result) {
        
        self.dicAll = result;
        
    
        
        NSArray *arr = _dicAll[@"recommenders"];
        
        NSArray *arrCom= _dicAll[@"comments"];
        
        self.arrLike = [GameXangQing ModelHandleWithArray:arr];
        self.arrCom = [GameXiangQingPingLun ModelHandleWithArray:arrCom];
        
        
        [_tableView reloadData];
    } withFailure:^(id error) {
        
    }];
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
